package pt.isel.lercm.projectmanager.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

public class Utils {
	
	
	public static String generateMd5(String word){
		
		String generatedWord = null;
		
		try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(word.getBytes());
            
            byte[] bytes = md.digest();
            
            StringBuilder sb = new StringBuilder();
            for(int i=0; i< bytes.length ;i++)
            {
                sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
            }
           
            generatedWord = sb.toString();
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }
		
		return generatedWord;
	}

	public static String generatePassword() {
		StringBuilder sb = new StringBuilder();
		int n = 6;
		String set = "abcdefghijlmnopqrstuvxzABCDEFGHIJLMNOPQRSTUVXZ1234567890";

		Random randomGenerator = new Random();
		for (int i= 0; i < n; i++) {
		    int k = randomGenerator.nextInt(set.length()-1);
		    sb.append(set.charAt(k));
		}
		return sb.toString();
	}
}
